package cn.virens.database.serviceimpl.sys;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.virens.database.common.BaseServiceImpl;
import cn.virens.database.common.ExampleWrapper;
import cn.virens.database.mysql.mapper.sys.SysUserMapper;
import cn.virens.database.mysql.model.sys.SysUser;
import cn.virens.database.service.sys.SysUserService;
import cn.virens.exception.APIException;
import cn.virens.util.Assert;

/**
 * 用户管理 - sys_user
 *
 * @author Virens
 * @since  2018-08-25 15:39:30
 */
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser> implements SysUserService {
	private static final String TEMP_PREFIX = "TEMP:";

	public SysUserServiceImpl() {
		super(SysUser.class);
	}

	@Override
	@Transactional(readOnly = true)
	public SysUser getUser(String account) {
		return mBaseMapper.findUser(Assert.isEmpty(account, "账号不可为空"));
	}

	@Override
	public SysUser insert(SysUser record) {
		record.setOpenid(format(record.getOpenid()));
		record.setAccount(format(record.getAccount()));

		return super.insert(record);
	}

	@Override
	@Transactional(readOnly = false)
	public SysUser update(SysUser record) {
		if (!StrUtil.isAllEmpty(record.getOpenid(), record.getAccount())) {
			ExampleWrapper example = ExampleWrapper.of(clazz);
			example.or((criteria) -> {
				criteria.orEqualTo("openid", record.getOpenid());
				criteria.orEqualTo("account", record.getAccount());
			});

			// 过滤受保护字段
			record.setId(null);
			record.setOpenid(null);
			record.setAccount(null);
			record.setCreateTime(null);
			record.setUpdateTime(DateUtil.date());

			// 执行数据更新
			if (mBaseMapper.updateByExampleSelective(record, example) > 0) {
				return record;
			} else {
				return null;
			}
		} else {
			throw new APIException("Error", "用户无效");
		}

	}

	@Override
	@Transactional(readOnly = false)
	public int delete(Serializable account) {
		ExampleWrapper example = ExampleWrapper.of(clazz);
		example.or((criteria) -> {
			criteria.orEqualTo("openid", account);
			criteria.orEqualTo("account", account);
		});

		return mBaseMapper.deleteByExample(example);
	}

	@Override
	@Transactional(readOnly = true)
	public List<SysUser> selectAll(Map<String, Object> param) throws APIException {
		return mBaseMapper.selectSimple(param);
	}

	@Override
	public SysUser selectOne(Serializable id) {
		return getUser(id == null ? null : id.toString());
	}

	@Override
	@Transactional(readOnly = false)
	public int bindOpenid(SysUser record) {
		Assert.isEmpty(record.getOpenid(), "授权ID不可为空");
		Assert.isEmpty(record.getAccount(), "账号不可为空");

		ExampleWrapper example = ExampleWrapper.of(clazz);
		example.or((criteria) -> {
			criteria.orEqualTo("account", record.getAccount());
		});

		record.setAccount(null);
		record.setCreateTime(null);
		record.setUpdateTime(DateUtil.date());

		return mBaseMapper.updateByExampleSelective(record, example);
	}

	@Override
	@Transactional(readOnly = false)
	public int bindAccount(SysUser record) {
		Assert.isEmpty(record.getOpenid(), "授权ID不可为空");
		Assert.isEmpty(record.getAccount(), "账号不可为空");

		ExampleWrapper example = ExampleWrapper.of(clazz);
		example.or((criteria) -> {
			criteria.orEqualTo("openid", record.getOpenid());
		});

		record.setOpenid(null);
		record.setCreateTime(null);
		record.setUpdateTime(DateUtil.date());

		return mBaseMapper.updateByExampleSelective(record, example);
	}

	private static String format(String str) {
		if (str == null || str.isEmpty()) {
			return TEMP_PREFIX + RandomUtil.randomString(32);
		} else {
			return str;
		}
	}

}
